// *********************************************************************
//  
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>
// 
// *********************************************************************

Strophe.addConnectionPlugin('gestion_roster', {

  init: function (conexion) {
    this.conexion = conexion;
  },

  //6.1. Requesting a Subscription
  suscribir: function (jid, nombre) {
    this.agregar_contacto(jid, nombre);
    this.aceptar_suscripcion(jid);
    this.conexion.send($pres({to: jid, type: "subscribe"}));

  },

  //6.2. Handling a Subscription Request
  aceptar_suscripcion: function (jid) {
    this.conexion.send($pres({to: jid, type: "subscribed"}));
  },

//   denegar_suscripcion: function (jid) {
//     this.conexion.send($pres({to: jid, type: "unsubscribed"}));
//   },

  //6.3. Cancelling a Subscription from Another Entity
  cancelar_su_suscripcion: function (jid) {
    this.conexion.send($pres({to: jid, type: "unsubscribed"}));
  },

  //6.4. Unsubscribing from Another Entity's Presence
  cancelar_tu_suscripcion: function (jid) {
    this.conexion.send($pres({to: jid, type: "unsubscribe"}));
    //si cancelas tu suscripcion a su presencia, para que lo quieres en el roster?
    //this.eliminar_contacto(jid);
  },

  //7.3. Retrieving One's Roster on Login
  obtener_roster: function () {
    this.conexion.sendIQ($iq({type: "get"}).c("query", {xmlns: Strophe.NS.ROSTER}));
  },

  //7.4. Adding a Roster Item
  agregar_contacto: function (jid, nombre) {
    this.conexion.sendIQ($iq({type: "set"}).c("query", {xmlns: Strophe.NS.ROSTER}).c("item", {jid: jid, name: nombre || ""}));
  },

  //7.5. Updating a Roster Item
  modificar_contacto: function (jid, nombre){
    this.conexion.sendIQ($iq({type: "set"}).c("query", {xmlns: Strophe.NS.ROSTER}).c("item", {jid: jid, name: nombre || ""}));
  },

  //7.6. Deleting a Roster Item
  eliminar_contacto: function (jid) {
    this.conexion.sendIQ($iq({type: "set"}).c("query", {xmlns: Strophe.NS.ROSTER}).c("item", {jid: jid, subscription: "remove"}));
  }

});
